#ifndef EEPROM_H_
#define EEPROM_H_

#include <iostream>
#include <ecrt.h>
#include "ceeprom.h"

/**
 * @Class:
 *      EEprom
 * @Berif:
 *      Read the eeprom content in the slave and parse it from the sync and pod regs information of the corresponding slave.
 */
class EEprom
{
public:
    EEprom();
    ~EEprom();

    /**
     * @Fn:
     *      eeprom_get_slave_sync
     * @Berif:
     *      Read the eeprom content and parse out the sync of the corresponding slave according to the content.
     * @Input:
     *      slave_index: the index of current slave
     *      slave_sync : the sync address of slave
     * @Output:
     *      success, return 0; or, return -1
     */
    bool GetSyncInfo(ec_sync_info_t** slave_sync, uint32_t slave_index, ec_master_t* master);

private:
    int eeprom_adjust_sync(ec_master_t* master, uint32_t slave_index, ec_sync_info_t* slave_sync);

    uint8_t* eeprom_get_slave_sii_words(int master_fd, uint32_t slave_index);

    int32_t eeprom_aggr_create_and_init(uint8_t *eeprom_data, eeprom_aggr_sync_t **aggr_sync);

    uint8_t* eeprom_locate_start_of_conf(uint8_t *data, uint16_t cfg);

    uint16_t eeprom_abtain_sync_count(uint8_t *data);

    uint16_t eeprom_abtain_pdo_count_of_sync(uint8_t *data, uint16_t sm_idx);

    int eeprom_aggr_sync_init(uint8_t *data, eeprom_aggr_sync_t *aggr_sync, uint16_t sync_count);

    int eeprom_aggr_pdo_init(uint8_t *data, eeprom_aggr_sync_t *aggr_sync, uint16_t sync_count);

    int eeprom_aggr_init(uint8_t *data, eeprom_aggr_sync_t *aggr_sync, uint16_t sync_count);

    int eeprom_dsync_get_pdos_entry_count(eeprom_aggr_sync_t *aggr_sync);

    int eeprom_dsync_sync_allocate(eeprom_aggr_sync_t *aggr_sync, ec_sync_info_t **sync);

    int eeprom_dsync_sync_init(eeprom_aggr_sync_t *aggr_sync, ec_sync_info_t *sync);

    int eeprom_dsync_get_sync_pdos_entry_count(eeprom_aggr_sync_t *aggr_sync, uint16_t sm_idx);

    int eeprom_dsync_pdo_and_entries_init(eeprom_aggr_sync_t *aggr_sync, ec_sync_info_t *sync);

    int eeprom_realloc_rx_pdos_entries(uint8_t *nmem, ec_pdo_entry_info_t *tmp_entries, uint8_t *odd, uint8_t *even, int odd_cnt, int even_cnt, int spec_idx);

    int eeprom_realloc_tx_pdos_entries(uint8_t *nmem, ec_pdo_entry_info_t *tmp_entries, uint8_t *odd,uint8_t *even, int odd_cnt, int even_cnt);

    int eeprom_dsync_pdo_alloc_entries(ec_sync_info_t *sync);

    int eeprom_dsync_create_and_init(eeprom_aggr_sync_t *aggr_sync, ec_sync_info_t **sync);
};

#endif